﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Pub;
using System.IO;

namespace LZQXT.Test.Forms.Tree
{
    public partial class FDocDel : Form
    {
        public ADOTest ado1;
        public string s_ID;
        public List<TabPage> listTabPage = new List<TabPage>();
        public List<DataGridView> listGridView = new List<DataGridView>();

        public FDocDel()
        {
            InitializeComponent();
        }

        private void FDocDel_Load(object sender, EventArgs e)
        {
            dataGridView1.Dock = DockStyle.Fill;
            dataGridView2.Dock = DockStyle.Fill;
            dataGridView3.Dock = DockStyle.Fill;
            dataGridView4.Dock = DockStyle.Fill;
            tabControl1.Dock = DockStyle.Fill;

        }

        public void docList_Ref()
        {
            CDebug.EnterMethod("填充文件列表");
            try
            {
                dataGridView1.DataSource = null;
                dataGridView2.DataSource = null;
                dataGridView3.DataSource = null;
                dataGridView4.DataSource = null;

                tabPage1.Text = "---";
                tabPage2.Text = "---";
                tabPage3.Text = "---";

                if (s_ID != string.Empty)
                {
                    string strSQL = "SELECT * FROM P_PRODUCT_FILE WHERE PRODUCT_ID=" + s_ID;
                    ado1 = FDBTree_lzqx.ado1;
                    DataSet ds1 = ado1.sel1(strSQL);
                    CDebug.Send(strSQL + "\n--返回文件个数: " + ado1.ds_count);
                    if (!CDB.dsIsNull(ds1))
                    {
                        dataGridView1.DataSource = ds1.Tables[0];
                    }

                    list_Ref("");
                }
            }
            catch (System.Exception ex)
            {
                CDebug.SendError(ex.ToString());
            }

            CDebug.ExitMethod("填充文件列表");
        }

        /// <summary>
        /// 删除单个产品
        /// </summary>
        /// <param name="t_ID"></param>
        public void DelRow(string t_ID)
        {
            CDebug.EnterMethod("DocList.aspx: 删除文件");
            try
            {
                //删除多个文件及文件记录
                String strSQL4 = "SELECT * FROM P_PRODUCT_FILE WHERE PRODUCT_ID=" + s_ID;
                if (t_ID != string.Empty) strSQL4 += " AND P_ID=" + t_ID;
                DataSet ds4 = ado1.sel1(strSQL4);
                if (!CPub.dsIsNull(ds4))
                {
                    for (int i = 0; i < ds4.Tables[0].Rows.Count; i++)
                    {
                        DataRow dr4 = ds4.Tables[0].Rows[i];
                        String mFilePath = toolStripTextBox1.Text;
                        String sProductFileName = dr4["FILENAME"].ToString();

                        //分目录存储
                        DateTime dat1 = Convert.ToDateTime(dr4["DDate"]);
                        string sDirII = s_ID + "\\" + dat1.Year + "\\" + dat1.Month.ToString("D2") + "\\";

                        String fd = mFilePath + sDirII + sProductFileName;
                        if (!File.Exists(fd))
                            CDebug.Send("产品文件不存在：\"" + fd + "\"");
                        else
                        {
                            CDebug.Send("开始删除文件：\"" + fd + "\"");
                            File.Delete(fd);
                        }

                        //删除最后一个产品，期号-1
                        //string sNedition = dr4["Nedition"].ToString();
                        //CNedition.delNedition(i_ID.ToString(), sNedition);
                    }


                }

                String strSQL1 = "DELETE FROM P_PRODUCT_FILE WHERE PRODUCT_ID=" + s_ID;
                if (t_ID != string.Empty) strSQL1 += " AND P_ID=" + t_ID;
                CDebug.Send(strSQL1);
                ado1.cmd1(strSQL1);

                string strSQL = "DELETE FROM P_Rain_Date WHERE PRODUCT_ID=" + s_ID;
                if (t_ID != string.Empty) strSQL += " AND P_ID=" + t_ID;
                CDebug.Send(strSQL);
                ado1.cmd1(strSQL);

                strSQL = "DELETE FROM P_PF_WORKLOG WHERE PID=" + s_ID;
                if (t_ID != string.Empty) strSQL += " AND FNAME='" + ds4.Tables[0].Rows[0]["FILENAME"].ToString() + "'";
                ado1.cmd1(strSQL);

                //删除主表及细表数据
                String strSQL2 = "SELECT * FROM P_PRODUCTCONFIG WHERE ID=" + s_ID;
                DataSet ds2 = ado1.sel1(strSQL2);
                if (!CPub.dsIsNull(ds2))
                {
                    String sCust_Table = ds2.Tables[0].Rows[0]["Cust_Table"].ToString();

                    String strSQL3 = "DELETE FROM " + sCust_Table;
                    if (t_ID != string.Empty) strSQL3 += " WHERE ID=" + t_ID;
                    CDebug.Send(strSQL3);
                    ado1.cmd1(strSQL3);

                    String strSQL5 = "SELECT Distinct(TableIndex) FROM P_Product_GridPropert WHERE F_ID=" + s_ID + " ORDER BY TableIndex ASC";
                    CDebug.Send(strSQL5);
                    DataSet ds5 = ado1.sel1(strSQL5);
                    CDebug.Send("返回结果个数：" + ds5.Tables[0].Rows.Count);
                    if (ds5 != null && ds5.Tables.Count > 0 && ds5.Tables[0].Rows.Count > 0)
                    {
                        for (int i = 0; i < ds5.Tables[0].Rows.Count; i++)
                        {
                            String sCust_DetailTable = ds2.Tables[0].Rows[0]["Cust_DetailTable"].ToString() + "_" + i;
                            String strSQL6 = "DELETE FROM " + sCust_DetailTable;
                            if (t_ID != string.Empty) strSQL6 += " WHERE F_ID=" + t_ID;
                            CDebug.Send(strSQL6);
                            ado1.cmd1(strSQL6);
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                CDebug.Send("删除文件出错：", ex);
            }

            CDebug.ExitMethod("删除文件结束！");
            CDebug.ExitMethod("产品列表初始化结束：DocList.aspx！");

        }

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(e.RowBounds.Location.X,
                e.RowBounds.Location.Y, dataGridView1.RowHeadersWidth - 4, e.RowBounds.Height);

            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
                dataGridView1.RowHeadersDefaultCellStyle.Font,
                rectangle,
                dataGridView1.RowHeadersDefaultCellStyle.ForeColor,
                TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }

        private void btnDelSel_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                if (CPub.MsgBox("确定要删除吗？", "删除"))
                {
                    for (int i = dataGridView1.SelectedRows.Count - 1; i >= 0; i--)
                    {
                        string t_ID = dataGridView1.SelectedRows[i].Cells[4].Value.ToString();
                        DelRow(t_ID);
                    }
                }
            }
        }

        private void btnShowAll_Click(object sender, EventArgs e)
        {
            list_Ref("");
        }

        private void btnDelAll_Click(object sender, EventArgs e)
        {
            if (CPub.MsgBox("确定要删除吗？", "删除"))
            {
                DelRow("");
            }
        }

        /// <summary>
        /// 刷新列表
        /// </summary>
        /// <param name="t_ID"></param>
        public void list_Ref(string t_ID)
        {
            CDebug.EnterMethod("填充文件列表");
            try
            {
                string strSQL = "SELECT * FROM P_PRODUCT_FILE WHERE PRODUCT_ID=" + s_ID;
                if (t_ID != string.Empty) strSQL += " AND P_ID=" + t_ID;
                ado1 = FDBTree_lzqx.ado1;
                DataSet ds1 = ado1.sel1(strSQL);
                CDebug.Send(strSQL + "\n--返回文件个数: " + ado1.ds_count);

                tabPage1.Text = "P_Rain_Date";
                strSQL = "SELECT * FROM P_Rain_Date WHERE PRODUCT_ID=" + s_ID;
                if (t_ID != string.Empty) strSQL += " AND P_ID=" + t_ID;
                DataSet ds2 = ado1.sel1(strSQL);
                CDebug.Send(strSQL + "\n--返回文件个数: " + ado1.ds_count);
                if (!CDB.dsIsNull(ds2))
                    dataGridView2.DataSource = ds2.Tables[0];
                else
                    dataGridView2.DataSource = null;

                tabPage2.Text = "P_PF_WORKLOG";
                strSQL = " SELECT * FROM P_PF_WORKLOG WHERE PID=" + s_ID;
                if (t_ID != string.Empty) strSQL += " AND FNAME='" + ds1.Tables[0].Rows[0]["FILENAME"].ToString() + "'";
                DataSet ds3 = ado1.sel1(strSQL);
                CDebug.Send(strSQL + "\n--返回文件个数: " + ado1.ds_count);
                if (!CDB.dsIsNull(ds3))
                    dataGridView3.DataSource = ds3.Tables[0];
                else
                    dataGridView3.DataSource = null;

                tabPage3.Text = "---";
                String strSQL2 = "SELECT * FROM P_PRODUCTCONFIG WHERE ID=" + s_ID;
                DataSet ds4 = ado1.sel1(strSQL2);
                if (!CPub.dsIsNull(ds4))
                {
                    String sCust_Table = ds4.Tables[0].Rows[0]["Cust_Table"].ToString();

                    tabPage3.Text = sCust_Table;
                    String strSQL3 = "SELECT * FROM " + sCust_Table + " WHERE 1=1 ";
                    if (t_ID != string.Empty) strSQL3 += " AND ID=" + t_ID;
                    CDebug.Send(strSQL3);
                    DataSet ds5 = ado1.sel1(strSQL3);
                    CDebug.Send(strSQL3 + "\n--返回文件个数: " + ado1.ds_count);
                    if (!CDB.dsIsNull(ds5))
                        dataGridView4.DataSource = ds5.Tables[0];
                    else
                        dataGridView4.DataSource = null;

                    for (int i = 0; i < listTabPage.Count; i++)
                    {
                        listTabPage[i].Text = "---";
                        listGridView[i].DataSource = null;
                    }
                    String strSQL5 = "SELECT Distinct(TableIndex) FROM P_Product_GridPropert WHERE F_ID=" + s_ID + " ORDER BY TableIndex ASC";
                    DataSet ds6 = ado1.sel1(strSQL5);
                    CDebug.Send(strSQL5 + "\n--返回文件个数: " + ado1.ds_count);
                    if (!CDB.dsIsNull(ds6))
                    {
                        for (int i = 0; i < ds6.Tables[0].Rows.Count; i++)
                        {

                            String sCust_DetailTable = ds4.Tables[0].Rows[0]["Cust_DetailTable"].ToString() + "_" + i;
                            if (tabControl1.TabPages.Count - 3 <= i)
                            {
                                TabPage tabPage_Tem = new TabPage();
                                tabPage_Tem.Text = sCust_DetailTable;
                                tabControl1.TabPages.Add(tabPage_Tem);
                                listTabPage.Add(tabPage_Tem);

                                DataGridView dataGridView_Tem = new DataGridView();
                                dataGridView_Tem.Parent = tabPage_Tem;
                                dataGridView_Tem.Dock = DockStyle.Fill;
                                dataGridView_Tem.Show();
                                listGridView.Add(dataGridView_Tem);
                            }
                            else
                            {
                                tabControl1.TabPages[3 + i].Text = sCust_DetailTable;
                            }
                            String strSQL6 = "SELECT * FROM " + sCust_DetailTable + " WHERE 1=1 ";
                            if (t_ID != string.Empty) strSQL6 += " AND F_ID=" + t_ID;
                            CDebug.Send(strSQL6);
                            DataSet ds_Tem = ado1.sel1(strSQL6);
                            if (!CDB.dsIsNull(ds_Tem))
                                listGridView[i].DataSource = ds_Tem.Tables[0];
                            else
                                listGridView[i].DataSource = null;
                        }
                    }
                }

            }
            catch (System.Exception ex)
            {
                CDebug.SendError(ex.ToString());
            }

            CDebug.ExitMethod("填充文件列表");
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                string t_ID = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
                list_Ref(t_ID);
            }
        }

        private void btnChkFile_Click(object sender, EventArgs e)
        {
            try
            {
                String strSQL4 = "SELECT * FROM P_PRODUCT_FILE WHERE PRODUCT_ID=" + s_ID;
                DataSet ds4 = ado1.sel1(strSQL4);
                if (!CPub.dsIsNull(ds4))
                {
                    ds4.Tables[0].Columns.Add(new DataColumn("CheckFile"));
                    for (int i = 0; i < ds4.Tables[0].Rows.Count; i++)
                    {
                        DataRow dr4 = ds4.Tables[0].Rows[i];
                        String mFilePath = toolStripTextBox1.Text;
                        String sProductFileName = dr4["FILENAME"].ToString();

                        //分目录存储
                        DateTime dat1 = Convert.ToDateTime(dr4["DDate"]);
                        string sDirII = s_ID + "\\" + dat1.Year + "\\" + dat1.Month.ToString("D2") + "\\";

                        String fd = mFilePath + sDirII + sProductFileName;
                        if (!File.Exists(fd))
                            ds4.Tables[0].Rows[0]["CheckFile"] = "True";
                        else
                            ds4.Tables[0].Rows[0]["CheckFile"] = "";
                    }
                    dataGridView1.DataSource = ds4.Tables[0];
                }
            }
            catch (System.Exception ex)
            {
                CDebug.SendError(ex.ToString());
            }

        }


    }
}
